package com.tree.rpc.socket.service;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @ClassName com.tree.rpc.socket.service
 * Description: <类功能描述>. <br>
 * <p>
 * <使用说明>
 * </p>
 * @Author tree
 * @Date 2019-11-07 00:30
 * @Version 1.0
 */
public class RpcRequestHandler implements Runnable{
    private Socket socket;
    public RpcRequestHandler(Socket socket ){
        this.socket = socket;
    }

    @Override
    public void run() {
        ObjectInputStream ois =  null;
        ObjectOutputStream oos = null;
        try {
            ois = new ObjectInputStream(socket.getInputStream());
            Object reqObj = ois.readObject();
            //调用服务的分发模块进行服务调用
            Object response = ServiceDispatcher.dispatch(reqObj);
            oos =new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(response);
            oos.flush();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(ois!=null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
